<
html> <head> <title>Assignment 5 Arrays</title> <script language="JavaScript"></script> <style type="text/css">area
{ background-color: #C0C0C0;}
.
newStyle1 { background-color: #C0C0C0;}
</
style> </head> <body> <script>var
stateNames= ["Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia","Hawaii"
, "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan","Minnesota"
, "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York","North Carolina"
, "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota","Tennessee"
, "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"]; //initialize an array of names to hold the 50 statesvar
capitalNames = ["Montgomery", "Juneau", "Phoenix", "Little Rock", "Sacramento", "Denver", "Hartford", "Dover", "Tallahassee","Atlanta"
, "Honolulu", "Boise", "Springfield", "Indianapolis", "Des Moines", "Topeka", "Frankfort", "Baton Rouge", "Augusta", "Annapolis","Boston"
, "Lansing", "St. Paul", "Jackson", "Jefferson City", "Helena", "Lincoln", "Carson City", "Concord", "Trenton", "Santa Fe", "Albany","Raleigh"
, "Bismarck", "Columbus", "Oklahoma City", "Salem", "Harrisburg", "Providence", "Columbia", "Pierre", "Nashville", "Austin","Salt Lake City"
, "Montpelier", "Richmond", "Olympia", "Charleston", "Madison", "Cheyenne"]; //initialize an array of names to hold the 50 capitals var i=0; // counter initialization and it runs once var userInput=window.prompt("What is the capital of " + stateNames[i] , "?"); //this creates a prompt that allows the user to enter the capital of the state correctCount=0; //this initializes the loop counter so it begins to count through the states while (userInput.toLowerCase()!="exit"&&i<stateNames.length) // toLowerCase()to convert user input into lower case and use the loop to go through the state names array { if (userInput.toLowerCase()==capitalNames[i].toLowerCase()) //if userInput equals the corresponding capital name from the array {correctCount
++; //increase counter by one i++; //go to the next state userInput = window.prompt("Correct!!!! What is the capital of " + stateNames[i]+ "? Please type exit to stop." , "?"); //if your answer was correct, you will receive a new prompt asking you to try another } else { //start the second part of the if statement within the while statement i++; //if the input does not equal the capital name then go to the next state userInput = window.prompt("Sorry. The correct answer is " + capitalNames[i-1] + ". What is the capital of " + stateNames[i] + "? Type exit to stop." , "?"); //if your answer was incorrect, you will receive this new prompt stating the correct capital, then you can move on to the next state }}
document.write(
"You received " + correctCount + " out of " + i + " correct. Thank you for playing!"); </script></
body></
html>